package com.sq.partner.manager.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.IGatewayAppTranstypeDao;
import com.sq.partner.manager.mybatis.model.GatewayAppTranstype;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.util.LogUtil;

/**
 * 
 * @author Kang Zhiqiang
 *
 */
@Repository
public class GatewayAppTranstypeDaoImpl extends BaseDaoImpl implements IGatewayAppTranstypeDao {
    private static Logger logger =  LogUtil.getLog(GatewayAppTranstypeDaoImpl.class);
	
	@Override
	public void save(GatewayAppTranstype appTranstype)
			throws Exception {
		this.add("insertGatewayAppTranstype", appTranstype);
	}

	@Override
	public List<GatewayAppTranstype> queryByFkAppId(
			GatewayAppTranstype appTranstype) throws Exception {
		List<GatewayAppTranstype> appTranstypeList = this.queryList("queryGatewayAppTranstypeByMap", appTranstype);
		return appTranstypeList;
	}

	@Override
	public Result delByAppTranstype(Long appTransTypeId)
			throws Exception {
		boolean flag = true;
		
		if(null != appTransTypeId){
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			
			String sql = "select * from t_gateway_channel_transtype c where c.trans_code = (select a.trans_code from t_gateway_app_transtype a where a.transtype_id = ?) "+
	                                          "  and c.fk_channel_code in "+
	                                              " (select b.channel_code from t_gateway_channel_info b where b.fk_appid = (select a.fk_app_id from t_gateway_app_transtype a where a.transtype_id = ? ))";
			try{
				conn = this.getConnection();
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, appTransTypeId);
				pstmt.setLong(2,appTransTypeId);
				rs = pstmt.executeQuery();
				if(rs.next()){
					flag = false;
					return Result.error("删除失败，请先删除关联数据");
				}
			}finally{
				close(rs);
				close(pstmt);
				close(conn);
			}
		}
		
		if(flag){
			this.delete("delGatewayAppTranstypeById",appTransTypeId);
		}
		return Result.success("删除成功");
	}

	@Override
	public List<GatewayAppTranstype> queryNotUsedAppTranstype(
			GatewayChannelInfo channelInfo) throws Exception {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<GatewayAppTranstype> appTranstypeList = new ArrayList<GatewayAppTranstype>();
		
		String sql = "select * from t_gateway_app_transtype where fk_app_id = ? and trans_code not in("+
						" select trans_code from t_gateway_channel_transtype where fk_channel_code = ? and app_id = ?)";
		
		try {
			conn = this.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, channelInfo.getFkAppid());
			pstmt.setString(2, channelInfo.getChannelCode());
			pstmt.setString(3, channelInfo.getFkAppid());
			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				GatewayAppTranstype at = new GatewayAppTranstype();
				at.setTranstypeId(rs.getLong(1));
				at.setTransCode(rs.getString(2));
				at.setTransName(rs.getString(3));
				at.setFkAppId(rs.getString(4));
				
				appTranstypeList.add(at);
			}
		} finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					logger.info(e);
				}
			}
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					logger.info(e);
				}
			}
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					logger.info(e);
				}
			}
		}
		
		
		return appTranstypeList;
	}

	@Override
	public GatewayAppTranstype checkTransCodeIfUsedInOneApp(String appId,
			String transCode) throws Exception {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		GatewayAppTranstype appTranstype = null;
		
		String sql = "select * from t_gateway_app_transtype where fk_app_id = ? and trans_code = ?";
		
		try {
			conn = this.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,appId);
			pstmt.setString(2,transCode);
			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				appTranstype = new GatewayAppTranstype();
				appTranstype.setTranstypeId(rs.getLong(1));
				appTranstype.setTransCode(rs.getString(2));
				appTranstype.setTransName(rs.getString(3));
				appTranstype.setFkAppId(rs.getString(4));
			}
		} finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					logger.info(e);
				}
			}
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					logger.info(e);
				}
			}
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					logger.info(e);
				}
			}
		}
		
		
		return appTranstype;
	}

	@Override
	public void delByAppId(String appId) throws Exception {
		this.delete("delGatewayAppTranstypeByAppId",appId);
	}

}
